﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp26
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private bool isDrawging = false;
        private Point ePoint = new();
        private Point mPoint = new();
        private Canvas canvas;


        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label label = (System.Windows.Controls.Label)sender;

            ePoint.X = Canvas.GetLeft(label);
            ePoint.Y = Canvas.GetTop(label);

            if (canvas != null)
            {
                canvas = (Canvas)LogicalTreeHelper.GetParent(label);
            }
            mPoint = e.GetPosition(canvas);

            label.CaptureMouse();
            isDrawging = true;
        }

        private void Label_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawging)
            {
                Label label = (Label)sender;
                Point point = e.GetPosition (canvas);

                label.SetValue(Canvas.LeftProperty, ePoint.X + (point.X -mPoint.X));
                label.SetValue(Canvas.TopProperty, ePoint.Y + (point.Y -mPoint.Y));
            }
        }

        private void Label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDrawging)
            {
                Label label = (Label)sender;    
                label.ReleaseMouseCapture();

                isDrawging = false;
            }
        }
    }
}